﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Configuration;
using LeadExpress.Models.ViewPages;

namespace LeadExpress.Areas.MyAccount.Controllers
{
    public class BusinessListController : Controller
    {
        private static string fileName = "";
        public static string staFileName
        {
            get { return fileName; }
            set { fileName = value; }
        }

        // Create an instance of user manage
        LeadExpress.Repository.BusinessManagementRepository repository = new LeadExpress.Repository.BusinessManagementRepository();
        public static string BusinessImagesURL = ConfigurationManager.AppSettings["BusinessImagesURL"].ToString();

        // GET: /Management/BusinessList/Edit/id
        [Authorize(Roles = "ClientAdmin")]
        public ActionResult Index()
        {
            BusinessManagementModel model = new BusinessManagementModel();

            var user = new Repository.UserRepository().GetSingle(User.Identity.Name);
            Business business = repository.GetSingleByUser(user.ID);
            int id = (business != null) ? business.ID : 0;

            model = repository.GetModelByUser(Helpers.Constant.HOME, "ClientAdmin");
            model.BusinessInfo = business;

            return View(model);
        }

        //
        // GET: /Management/BusinessList/Edit/id
        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "ClientAdmin")]
        public ActionResult Index(FormCollection form)
        {
            var user = new Repository.UserRepository().GetSingle(User.Identity.Name);
            Business model = repository.GetSingleByUser(user.ID);

            if (model == null)
                model = new Business();

            model.CompanyName = form["CompanyName"].ToString();
            model.ContactTitle = form["ContactTitle"].ToString();
            model.BusinessTypeID = int.Parse(form["BusinessType"].ToString());
            model.Address1 = form["Address1"].ToString();
            model.Address2 = form["Address2"].ToString();
            model.City = form["City"].ToString();
            model.State = form["State"].ToString();
            model.ZipCode = form["ZipCode"].ToString();
            model.Phone = form["Phone"].ToString();
            model.Fax = form["Fax"].ToString();
            model.EmailAddress = form["EmailAddress"].ToString();
            model.ImageURL = form["ImageUrl"].ToString();
            model.Active = (form["Active"] != null && form["Active"].ToString() == "on");
            model.WebsiteURL = form["WebsiteURL"].ToString();

            if (model != null && model.ID > 0)
            {
                repository.Save(model);
            }
            else
            {
                model.CreateByUserID = new Repository.UserRepository().GetSingle(User.Identity.Name).ID;
                repository.Create(model);
            }

            return RedirectToAction("Index", "BusinessList");
        }

        //[HttpPost]
        public ActionResult Upload(HttpPostedFileBase fileData)
        {
            var user = new Repository.UserRepository().GetSingle(User.Identity.Name);

            // Verify that the user selected a file
            UserModel model = new UserModel();
            if (fileData != null && fileData.ContentLength > 0)
            {
                // extract only the fielname
                fileName = Path.GetFileName(fileData.FileName);
                fileName = string.Format("{0}-{1}", user.ID, fileName);

                // store the file inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath(BusinessImagesURL), fileName);
                fileData.SaveAs(path);
            }
            // redirect back to the index action to show the form once again
            return Json(model);
        }

        [HttpGet]
        public ActionResult UploadComplete()
        {
            return Json(string.Format("{0}/{1}", BusinessImagesURL, fileName), JsonRequestBehavior.AllowGet);
        }
    }
}
